﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsolidationTool.Common;
using System.Web;

namespace ConsolidationTool.WebParts
{
    public class DillReportBase:FSReportBase
    {

        public string PageTitle { get; set; }
        public string ActualListTitle { get; set; }
        public string ForecastListTitle { get; set; }
        public string BudgetListTitle { get; set; }
        public List<string> HeadRowColumns { get; set; }

        public bool isForecastDDReport { get { return ActualListTitle.IsNotNullOrEmpty() && ForecastListTitle.IsNotNullOrEmpty(); } }
        public bool isBudgetDDReport { get { return BudgetListTitle.IsNotNullOrEmpty(); } }
        public bool ifWithLastDec { get { return Request["ldec"].IsNotNullOrEmpty(); } }

        public List<RQDataItem> TableData = new List<RQDataItem>();

        public override void BeforLoad()
        {
            base.BeforLoad();

            PageTitle = HttpUtility.UrlDecode(Request["t"]);
            ActualListTitle = HttpUtility.UrlDecode(Request["a"]);
            ForecastListTitle = HttpUtility.UrlDecode(Request["f"]);
            BudgetListTitle = HttpUtility.UrlDecode(Request["b"]);

            if (!IsPostBack)
                loadConfigYearMonth();

        }

        public override void GenerateReport_Click(object sender, EventArgs e)
        {
            base.GenerateReport_Click(sender, e); 
        }

        public virtual void ReceiveEntityData(string coCd, int year, int month, string listTitle, bool forecast)
        {
            throw new NotImplementedException(); 
        }
        public virtual void CreateKoInstance(int month, string currency, fsentity p, bool forecast, int index)
        {
            throw new NotImplementedException();
        }

        public override void TemplateLocator()
        {
            var topHeaderRow = new RQDataItem() { head = "thead='true'", acountcode = "Accountcode", title = "Description", unit = "Unit" };
            var secondHeaderRow = new RQDataItem() { head = "thead='true'" };

            if (isForecastDDReport)
            {
                var startDate = CEntity.SelectedDateTime.AddMonths(-(CEntity.SelectedDateTime.Month - 1));

                for (int i = 0; i < CEntity.SelectedDateTime.Month + 3; i++)
                {
                    topHeaderRow.values.Add(startDate.AddMonths(i).ToString("yyyy.MM"));
                    secondHeaderRow.values.Add(i < CEntity.SelectedDateTime.Month ? "Actual" : "Forecast");
                }
                TableData.Add(topHeaderRow);

                TableData.Add(secondHeaderRow);
            }
            else if (isBudgetDDReport) {
                HeadRowColumns = new List<string>() { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
                if (ifWithLastDec)
                    HeadRowColumns.Insert(0, "L_Dec");

                topHeaderRow.values.AddRange(HeadRowColumns);
                TableData.Add(topHeaderRow);
            }
        }

        public virtual void AfterDataLoaded() 
        {
            //fix row empty column
            var realColumnLenght = CEntity.SelectedDateTime.Month + 3;
            if (isBudgetDDReport)
                realColumnLenght = HeadRowColumns.Count;

            TableData.ForEach(p =>
            {
                var needToFixColumn = realColumnLenght - p.values.Count;
                if (needToFixColumn > 0)
                {
                    for (int i = 0; i < needToFixColumn; i++)
                    {
                        p.values.Add("");
                    }
                }
            });

            BaseReport.DataSource = TableData;
            BaseReport.DataBind();
        }

        public virtual void loadConfigYearMonth()
        {
            TopSelector.yearTextBox.Text = BusinessService.GetConfiguration(Constants.Flash_Input_Year);
            TopSelector.monthTextBox.Text = BusinessService.GetConfiguration(Constants.Flash_Input_Month);
        }


    }
}
